package com.lin.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.lin.utils.JdbcUtils;
import com.lin.vo.Book;
import com.lin.vo.Order;
import com.lin.vo.OrderItem;
import com.lin.vo.User;

public class OrderDaoImpl implements IOrderDao {

	@Override
	public void save(Connection connection, Order order) {
		QueryRunner queryRunner = new QueryRunner();
		String sql = "INSERT INTO orders VALUES(?,?,?,?,?,?)";
		Object[] params = { order.getOid(), order.getTotal(), order.getOrdertime(), order.getState(),
				order.getAddress(), order.getUser().getUid() };

		try {
			queryRunner.update(connection, sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException();
		}

	}

	public void save(Connection connection, OrderItem orderItem) {
		QueryRunner queryRunner = new QueryRunner();
		String sql = "INSERT INTO orderitem VALUES(?,?,?,?,?)";
		Object[] params = { orderItem.getItemid(), orderItem.getCount(), orderItem.getSubtotal(),
				orderItem.getBook().getBid(), orderItem.getOrder().getOid() };
		try {
			queryRunner.update(connection, sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<Order> findByUid(User user) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "SELECT * FROM orders WHERE uid = ? ORDER BY ordertime DESC";
		List<Order> orderList;
		try {
			orderList = queryRunner.query(sql, new BeanListHandler<>(Order.class), user.getUid());
			for (Order order : orderList) {
				sql = "SELECT * FROM orderitem o INNER JOIN book b ON o.bid = b.bid AND o.oid = ?";
				List<Map<String, Object>> orderLists = queryRunner.query(sql, new MapListHandler(), order.getOid());
				for (Map<String, Object> map : orderLists) {
					// 1.将Book数据封装到 book对象中
					Book book = new Book();
					BeanUtils.populate(book, map);
					// 2将订单项的数据，封装到订单项对象中
					OrderItem orderItem = new OrderItem();
					BeanUtils.populate(orderItem, map);
					orderItem.setBook(book);
					orderItem.setOrder(order);
					// 将得到的数据，封装到订单的list集合中
					order.getOrderItems().add(orderItem);

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return orderList;
	}

	public Order findByOid(String oid) {
		QueryRunner query = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select * from orders where oid = ?";
		Order order;
		try {
			order = query.query(sql, new BeanHandler<>(Order.class), oid);
			sql = "select * from orderitem o, book b where o.bid = b.bid and o.oid = ?";
			List<Map<String, Object>> list = query.query(sql, new MapListHandler(), oid);
			for (Map<String, Object> map : list) {
				Book book = new Book();
				BeanUtils.populate(book, map);

				OrderItem orderItem = new OrderItem();
				BeanUtils.populate(orderItem, map);
				orderItem.setBook(book);
				orderItem.setOrder(order);

				// 放入订单对象的list集合中
				order.getOrderItems().add(orderItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return order;
	}

	@Override
	public void updateOrder(Order order) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "UPDATE orders SET total = ?, ordertime = ?, state = ?, address = ? WHERE oid = ?";
		Object[] params = { order.getTotal(), order.getOrdertime(), order.getState(), order.getAddress(),
				order.getOid() };
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}

	}

	@Override
	public List<Order> findByAllOrders() {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql ="SELECT * FROM orders";
		List<Order> orderLists;
		Book book =null;
		OrderItem orderItem = null;
		try {
			orderLists= queryRunner.query(sql, new BeanListHandler<>(Order.class));
			for(Order order : orderLists){
				sql = "SELECT * FROM orderitem o INNER JOIN book b ON o.bid=b.bid AND o.oid = ?";
				List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), order.getOid());
				for(Map<String, Object> map : list){
					// 封装数据
					book =new Book();
					BeanUtils.populate(book, map);
					orderItem =new OrderItem();
					BeanUtils.populate(orderItem, map);
					orderItem.setBook(book);
					orderItem.setOrder(order);
					
					order.getOrderItems().add(orderItem);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return orderLists;
	}

	@Override
	public List<Order> findByAllOrders(int state) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql ="SELECT * FROM orders WHERE state = ? ORDER BY ordertime desc";
		List<Order> orderLists;
		Book book = null;
		OrderItem orderItem = null;
		try {
			orderLists= queryRunner.query(sql, new BeanListHandler<>(Order.class),state);
			for(Order order : orderLists){
				sql ="SELECT * FROM orderitem o INNER JOIN book b ON o.bid=b.bid AND o.oid = ?";
				List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), order.getOid());
				for(Map<String, Object> map : list){
					book = new Book();
					BeanUtils.populate(book, map);
					orderItem = new OrderItem();
					BeanUtils.populate(orderItem, map);
					orderItem.setBook(book);
					orderItem.setOrder(order);
					order.getOrderItems().add(orderItem);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return orderLists;
	}
}
